package com.jbh.recordlibrary.v;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.Nullable;
import com.jbh.recordlibrary.R;
import com.jbh.recordlibrary.i.AddAudioRecordListener;
import com.jbh.recordlibrary.u.BitmapUtil;
import java.util.List;

public class RecordSliderWaveBitmapView extends LinearLayout implements View.OnTouchListener{

    private View view ;
    private RecordTimeView cutWaveRecordTimeView ;
    private RecordWaveView cutWaveRecordWaveView;
    private TextView cutWaveView ;
    private float currentCutX;
    private float lastCutX;
    private float lastCutDx;
    boolean isInterpretation ;
    private HorizontalScrollView mScrollView ;
    private float totalLength ;
    private float oneSecondLength;

    private int[] cut = new  int[2] ;

    private AddAudioRecordListener addAudioRecordListener;

    public void setAddAudioRecordListener(AddAudioRecordListener addAudioRecordListener) {
        this.addAudioRecordListener = addAudioRecordListener;
    }

    public void setSliderWaveColor( int sliderWaveColor){
        if (cutWaveView!=null){
            cutWaveView.setBackgroundColor(sliderWaveColor);
        }
    }


    public RecordSliderWaveBitmapView(Context context) {
        this(context,null);
    }


    public RecordSliderWaveBitmapView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }


    public RecordSliderWaveBitmapView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        addView(view = LayoutInflater.from(context).inflate(R.layout.slider_wave_bitmap_layout,this,false));
        cutWaveRecordTimeView = view.findViewById(R.id.cutWaveRecordTimeView);
        cutWaveRecordWaveView = view.findViewById(R.id.cutWaveRecordWaveView);
        cutWaveView = view.findViewById(R.id.cutWaveView);
        cutWaveView.setOnTouchListener(this);
        mScrollView = view.findViewById(R.id.mScrollView);
    }


    /**
     * 设置录音数据
     *
     * @param divider
     * @param oneSecondLength
     * @param totalBuf
     * @param totalTime
     * @param totalLength
     */
    public void setRecordData(float divider,
                              float oneSecondLength,
                              List<Short> totalBuf,
                              long totalTime,
                              float totalLength) {

        this.oneSecondLength=oneSecondLength;
        this.totalLength = totalLength;

        ViewGroup.LayoutParams params = cutWaveRecordWaveView.getLayoutParams();;
        params.width = (int) totalLength;
        cutWaveRecordWaveView.setLayoutParams(params);
        cutWaveRecordWaveView.setData(divider,totalBuf);
        cutWaveRecordTimeView.setData(oneSecondLength,totalTime);

        ViewGroup.LayoutParams p = cutWaveView.getLayoutParams();
        p.width = waveWidth();
        cutWaveView.setLayoutParams(p);

        resetCutVoiceView();
    }



    public int waveWidth(){
        return (int) Math.ceil((oneSecondLength * 5)) ;
    }



    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        if (view.getId() == R.id.cutWaveView){
            cutVoiceView(motionEvent) ;
        }
        return isInterpretation;
    }



    /**
     * 滑块剪切功能
     *
     * @param event
     */
    private void cutVoiceView(MotionEvent event) {
        float x = event.getRawX();
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                cutWaveView.getParent().requestDisallowInterceptTouchEvent(true);
                isInterpretation=true;
                lastCutX = x ;
                break;

            case MotionEvent.ACTION_MOVE:
                /**当前移动的距离*/
                currentCutX = x - lastCutX + lastCutDx ;
                /**左边界*/
                if (currentCutX<0){
                    currentCutX = 0 ;
                }
//                /**右边界*/
                if (currentCutX + cutWaveView.getWidth()>totalLength){
                    currentCutX = totalLength - cutWaveView.getWidth();
                }
                cutWaveView.setTranslationX(currentCutX);
                break;

            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_UP:
                cutWaveView.getParent().requestDisallowInterceptTouchEvent(false);
                isInterpretation=false;
                lastCutDx = currentCutX;

                cut[0]= (int) currentCutX;
                cut[1]= (int) currentCutX+cutWaveView.getWidth();
                /**生成指定区域的图片*/
                cutWaveToBitmap();
                break;
        }
    }


    /**
     * 重置滑块
     */
    public void resetCutVoiceView(){
        /**返回后重置滑块的位置*/
        cutWaveView.setTranslationX(0);
        /**当前滑动的位置*/
        currentCutX= 0 ;
        /**上一次滑动结束的位置*/
        lastCutX= 0 ;
        /**上一次移动的位置*/
        lastCutDx= 0 ;
        mScrollView.scrollTo(0,0);

        cut[0] = 0 ;
        cut[1] = waveWidth();
    }



    /**剪切图片*/
    private void cutWaveToBitmap() {
        try {
            /**把view转换成bitmap*/
            Bitmap b = BitmapUtil.captureView(cutWaveRecordWaveView);
            /**在原图中剪切范围*/
            Rect rect=new Rect(cut[0], 0+2, cut[1], cutWaveRecordWaveView.getHeight()-2);
            /**剪切得到的图*/
            Bitmap bitmap=BitmapUtil.ImageCrop(b,rect);
            if (addAudioRecordListener!=null){
                addAudioRecordListener.onWaveBitmap(bitmap);
            }
            /**设置到戒臂上*/
            b.recycle();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            if (addAudioRecordListener!=null){
                addAudioRecordListener.onWaveBitmap(null);
            }
        }
    }
}
